﻿/*----------------------------------------------------------------
// Copyright (C) 年份 北京大象科技有限公司
// 版权所有。 	
//
// 文件名：RawBufferReader.cs
// 文件功能描述：
//
//
// 创建标识：gaohongwei 2016/11/8 16:50:58
//
// 修改标识：
// 修改描述：
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Score.SharedLibrary
{
    /// <summary>
    /// buffer读取类，以游标的方式读取byte数组，非线程安全，不要在多线程之间共享对象
    /// </summary>
    public class RawBufferReader
    {
        private readonly byte[] _data;
        private int _index;

        public RawBufferReader(byte[] data, int index = 0)
        {
            _data = data;
            _index = index;
        }

        /// <summary>
        /// 获取或设置 当前读取的字节索引位置
        /// </summary>
        public int Position
        {
            get { return _index; }
            set { _index = value; }
        }

        public byte ReadByte()
        {
            return _data[_index++];
        }

        public short ReadInt16()
        {
            short ret = BitConverter.ToInt16(_data, _index);
            _index += sizeof(short);
            return ret;
        }

        public ushort ReadUInt16()
        {
            var ret = BitConverter.ToUInt16(_data, _index);
            _index += sizeof(ushort);
            return ret;
        }

        public int ReadInt32()
        {
            var ret = BitConverter.ToInt32(_data, _index);
            _index += sizeof(int);
            return ret;
        }

        public uint ReadUInt32()
        {
            var ret = BitConverter.ToUInt32(_data, _index);
            _index += sizeof(uint);
            return ret;
        }

        public long ReadInt64()
        {
            var ret = BitConverter.ToInt64(_data, _index);
            _index += sizeof(long);
            return ret;
        }

        public ulong ReadUInt64()
        {
            var ret = BitConverter.ToUInt64(_data, _index);
            _index += sizeof(ulong);
            return ret;
        }

        public double ReadDouble()
        {
            var ret = BitConverter.ToDouble(_data, _index);
            _index += sizeof(double);
            return ret;
        }

        public float ReadFloat()
        {
            var ret = BitConverter.ToSingle(_data, _index);
            _index += sizeof(float);
            return ret;
        }

        public char ReadChar()
        {
            var ret = BitConverter.ToChar(_data, _index);
            _index += sizeof(char);
            return ret;
        }
    }
}
